package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 新建分组
 * Created by ukaodev on 2017/6/2.
 */

public class BuildGroupDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.edit_name_et)
    EditText editNameEt;
    @BindView(R.id.edit_name_cancel)
    Button editNameCancel;
    @BindView(R.id.edit_name_submit)
    Button editNameSubmit;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    private OnDialogSureClickListener mOnDialogSureClickListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_build_group_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initParams();
        return view;
    }


    private void initParams() {
        title.setText("新建分組");
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        Bundle bundle = getArguments();


    }


    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }

    @OnClick({R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case   R.id.close_btn:
                dismiss();
                break;

            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.edit_name_submit:
                String streditNameEt = getText(editNameEt);
                if (streditNameEt.isEmpty()) return;
                hideKeyboard();
                if (mOnDialogSureClickListener != null) {
                    mOnDialogSureClickListener.onSureItemClick(streditNameEt);
                }

                dismiss();
                break;
        }
    }


}
